<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/7/31
 * Time: 20:58
 */
namespace App\Services;

use Illuminate\Support\Facades\Storage;

class UploadOSSorStorage
{
    // 只允许以下后缀名的图片文件上传
    protected $allowedExt = ["image/png", "image/jpg", "image/gif", 'image/jpeg'];

    /**
     * 判断本地文件是否上传成功
     * @return bool
     */
    public function isValidFile($file)
    {
        if (!empty($file) && $file->isValid()) {
            return true;
        }
        return false;
    }

    /**
     * 验证文件类型是否有效
     * @param $file
     * @return bool
     */
    public function isValidFileType($file)
    {
        $type = $file->getClientMimeType();
        if (!in_array($type, $this->allowedExt)) {
            return false;
        }
        return true;
    }

    /**
     * 验证文件大小是否超出限制
     * @param $file
     * @param $fileLimitSize
     * @return bool
     */
    public function isValidFileSize($file, $fileLimitSize)
    {
        $fileSize = $file->getSize();
        //上传文件大小不能超过2M
        if ($fileSize > $fileLimitSize) {
            return false;
        }
        return true;
    }

    /**
     * 上传文件到阿里云oss或者上传到本地存储
     * @param $file
     * @param int $fileLimitSize
     * @param string $uploadWhere
     * @param string $folder
     * @return bool|string
     */
    public function uploadOSSorStorageLocal($file, $fileLimitSize = 2048000, $uploadWhere = "storage", $folder = "")
    {
        // 本地文件是否上传成功
        if (!$this->isValidFile($file)) return false;
        //上传文件类型
        if (!$this->isValidFileType($file)) return false;
        //验证文件大小是否超出限制
        if (!$this->isValidFileSize($file, $fileLimitSize)) return false;
        $ext = $file->getClientOriginalExtension();     // 扩展名
        $realPath = $file->getRealPath();   //临时文件的绝对路径
        // 上传文件
        $filename = time() . rand(10000, 99999999) . '.' . $ext;
        //阿里 OSS 图片上传
        if ('alioss' == $uploadWhere) {
            $key = 'uploads/' . $folder . '/' . date('Y-m-d') . '/' . $filename;
            $storage = OSS::upload($key, $realPath);
            //存入文件夹是否成功
            if (!$storage) {
                return false;
            } else {
                return $key;
            }
        }
        //本地存储
        if ('storage' == $uploadWhere) {
            $storage = Storage::disk($folder)->put($filename, file_get_contents($realPath));
            //存入文件夹是否成功
            if (!$storage) {
                return false;
            } else {
                return 'uploads/' . $folder . '/' . date('Y-m-d') . '/' . $filename;
            }
        }
        return false;
    }

    public function uploadOSSorStorageLocalApi($file, $fileLimitSize = 2048000, $uploadWhere = "storage", $folder = "")
    {
        //上传文件类型
        if (!$this->isValidFileType($file)) return false;
        //验证文件大小是否超出限制
        if (!$this->isValidFileSize($file, $fileLimitSize)) return false;
        $ext = $file->getClientOriginalExtension();     // 扩展名
        $realPath = $file->getRealPath();   //临时文件的绝对路径
        // 上传文件
        $filename = time() . rand(10000, 99999999) . '.' . $ext;
        //阿里 OSS 图片上传
        if ('alioss' == $uploadWhere) {
            $key = 'uploads/' . $folder . '/' . date('Y-m-d') . '/' . $filename;
            $storage = OSS::upload($key, $realPath);
            //存入文件夹是否成功
            if (!$storage) {
                return false;
            } else {
                return $key;
            }
        }
        //本地存储
        if ('storage' == $uploadWhere) {
            $storage = Storage::disk($folder)->put($filename, file_get_contents($realPath));
            //存入文件夹是否成功
            if (!$storage) {
                return false;
            } else {
                return 'uploads/' . $folder . '/' . date('Y-m-d') . '/' . $filename;
            }
        }
        return false;
    }

    /**
     * 删除旧图片
     * @param $filePath
     * @return mixed
     */
    public function deleteOSSorStorageLocal($filePath, $uploadWhere = "storage")
    {
        if ('alioss' == $uploadWhere) {
            return OSS::deleteObject($filePath);
        }
        if ('storage' == $uploadWhere) {
            return Storage::disk('delete')->delete($filePath);
        }
        return false;
    }
}